package memory;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


@SuppressWarnings("serial")
public class OptionsWindow extends JFrame implements ActionListener {

	private MainWindow mwRef;
	
	private JPanel leftPanel = new JPanel(new GridLayout(5,1));
	private JPanel rightPanel = new JPanel(new GridLayout(4,1));
	
	private JPanel rightPanel0 = new JPanel(new FlowLayout());
	private JPanel rightPanel1 = new JPanel(new FlowLayout());
	private JPanel rightPanel2 = new JPanel(new FlowLayout());
	private JPanel rightPanel3 = new JPanel(new FlowLayout());
	
	private TextField player1Field = new TextField();
	private TextField player2Field = new TextField();
	private TextField player3Field = new TextField();
	private TextField player4Field = new TextField();
	private TextField imgDir = new TextField();

	private SpinnerNumberModel rowsMod = new SpinnerNumberModel(Options.getRows(),1,10,1);
	private SpinnerNumberModel colsMod = new SpinnerNumberModel(Options.getCols(),1,10,1);
	private JSpinner rowsSpinner = new JSpinner(rowsMod);
	private JSpinner colsSpinner = new JSpinner(colsMod);
	
	private JButton ok = new JButton("Ok");
	private JButton cancel = new JButton("Cancel");

	private JLabel playersLabel = new JLabel("Players");
	private JLabel rowsLabel = new JLabel("Rows");
	private JLabel colsLabel = new JLabel("Columns");
	
	
	public OptionsWindow(MainWindow mw) {

		//setIconImage(Toolkit.getDefaultToolkit().getImage("./data/1.png"));
		
		mwRef = mw;
		mwRef.setEnabled(false);
		
		String[] playerNames = Options.getNames();
		
		player1Field.setText(playerNames[0]);
		player2Field.setText(playerNames[1]);
		player3Field.setText(playerNames[2]);
		player4Field.setText(playerNames[3]);
		
		imgDir.setText(Options.getImgDir());
		
		setSize(new Dimension(400,200));
		setLayout(new GridLayout(1,2)); 

		rowsMod.setValue(Options.getRows());
		colsMod.setValue(Options.getCols());
		
		add(leftPanel);
		add(rightPanel);
		
		leftPanel.add(playersLabel);
		leftPanel.add(player1Field);
		leftPanel.add(player2Field);
		leftPanel.add(player3Field);
		leftPanel.add(player4Field);
		
		rightPanel.add(rightPanel0);
		rightPanel.add(rightPanel1);
		rightPanel.add(rightPanel2);
		rightPanel.add(rightPanel3);
		
		rightPanel1.add(rowsLabel);
		rightPanel1.add(rowsSpinner);
		
		rightPanel2.add(colsLabel);
		rightPanel2.add(colsSpinner);
		
		rightPanel3.add(ok);
		rightPanel3.add(cancel);
						
		ok.addActionListener(this);
		cancel.addActionListener(this);
		setTitle("Options");
		
		setLocationRelativeTo(mw);
		setVisible(true);
					
	}
	
	public void actionPerformed(ActionEvent e) {
		int r = rowsMod.getNumber().intValue();
		int c = colsMod.getNumber().intValue();
		String[] temp = { player1Field.getText().trim() , player2Field.getText().trim() , player3Field.getText().trim() , player4Field.getText().trim() };		

		if (e.getSource() == ok) { 
			
			if ((r*c) % 2 != 0 ) {
				JOptionPane.showMessageDialog(this, "Number of cards must be even!");
			}
			
			else if ( (r*c) > Tools.getImgArray().length*2 ) {
				JOptionPane.showMessageDialog(this, "Number of cards exceeding number of images in img-dir! (max " + Tools.getImgArray().length*2 + ")" );
			}
			
			else if (temp[0].length() == 0 && temp[1].length() == 0 && temp[2].length() == 0 && temp[3].length() == 0) {
				JOptionPane.showMessageDialog(this, "At least one player must be entered!");
			}
		
			else {
				Options.setRows(r);
				Options.setCols(c);
				Options.setNames(temp);
											
				this.dispose();
				Point p = mwRef.getLocation();
				mwRef.dispose();
				new MainWindow(p);
			}
		}
		else if (e.getSource() == cancel) {
			mwRef.setEnabled(true);
			this.dispose();
		}
	}

}
